/*
 Navicat Premium Dump SQL

 Source Server         : docker
 Source Server Type    : MySQL
 Source Server Version : 80042 (8.0.42)
 Source Host           : localhost:3306
 Source Schema         : med_order_db

 Target Server Type    : MySQL
 Target Server Version : 80042 (8.0.42)
 File Encoding         : 65001

 Date: 13/08/2025 10:32:22
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for order_master
-- ----------------------------
DROP TABLE IF EXISTS `order_master`;
CREATE TABLE `order_master` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
  `order_no` varchar(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci NOT NULL COMMENT '订单号；唯一',
  `user_id` bigint NOT NULL COMMENT '下单用户ID',
  `patient_id` bigint DEFAULT NULL COMMENT '就诊人ID',
  `order_type` tinyint NOT NULL COMMENT '1碎片/2排班/3线下预约/4处方',
  `amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '应付金额（元）',
  `status` tinyint NOT NULL DEFAULT '0' COMMENT '0未支付/1已支付/2退款中/3已退款/4已关闭',
  `pay_deadline` datetime DEFAULT NULL COMMENT '支付截止（防恶意占号）',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '下单时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `order_no` (`order_no`),
  KEY `idx_order_no` (`order_no`),
  KEY `idx_type_status` (`order_type`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci COMMENT='订单主表';

-- ----------------------------
-- Records of order_master
-- ----------------------------
BEGIN;
COMMIT;

-- ----------------------------
-- Table structure for payment_record
-- ----------------------------
DROP TABLE IF EXISTS `payment_record`;
CREATE TABLE `payment_record` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
  `order_id` bigint NOT NULL COMMENT '订单ID → order_master.id',
  `pay_channel` tinyint NOT NULL COMMENT '1微信/2支付宝/3银行卡/9其他',
  `out_trade_no` varchar(64) NOT NULL COMMENT '第三方交易号',
  `amount` decimal(10,2) NOT NULL COMMENT '实付金额（元）',
  `status` tinyint NOT NULL DEFAULT '0' COMMENT '0待支付/1成功/2失败',
  `paid_at` datetime DEFAULT NULL COMMENT '支付时间（成功时）',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录时间',
  PRIMARY KEY (`id`),
  KEY `idx_order` (`order_id`),
  KEY `idx_out_trade` (`out_trade_no`),
  CONSTRAINT `fk_payment_order` FOREIGN KEY (`order_id`) REFERENCES `order_master` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `payment_record_chk_1` CHECK ((`status` in (0,1,2))),
  CONSTRAINT `payment_record_chk_2` CHECK ((`pay_channel` in (1,2,3,9)))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='支付记录';

-- ----------------------------
-- Records of payment_record
-- ----------------------------
BEGIN;
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;
